package fr.univ.surveyonline.model;

import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

import fr.univ.surveyonline.datastore.PMF;
import fr.univ.surveyonline.model.allEnum.Categorie;
import fr.univ.surveyonline.model.allEnum.Region;
import fr.univ.surveyonline.model.allEnum.Role;
import fr.univ.surveyonline.model.allEnum.SexeEnum;

@PersistenceCapable(identityType = IdentityType.DATASTORE)
public class User {

	// ATTRIBUTS
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String login;
	
	@Persistent
	private String password;
	
	@Persistent
	private boolean isValid;
	
	@Persistent
	private String firstName;

	@Persistent
	private String lastName;

	@Persistent
	private SexeEnum sexe;
	
	@Persistent
	private String mail;

	@Persistent
	private Date birthday;

	@Persistent
	private Region region;

	@Persistent
	private Categorie categorie;

	@Persistent
	private Role role;

	@Persistent
	private int money;

	// REQUETES
	public Key getKey()	{
		return key;
	}

	public String getLogin()
	{
		return login;
	}
	
	public String getPassword()
	{
		return password;
	}
	
	public boolean isValid()
	{
		return isValid;
	}
	
	public String getFirstName()
	{
		return firstName;
	}

	public String getLastName()	{
		return lastName;
	}

	public SexeEnum getSexe()	{
		return sexe;
	}
	
	public String getMail()
	{
		return mail;
	}

	public Region getRegion()
	{
		return region;
	}

	public Categorie getCategorie()
	{
		return categorie;
	}

	public Role getRole()
	{
		return role;
	}

	public Date getBirthday()
	{
		return birthday;
	}

	public int getMoney() {
		return money;
	}

	// METHODES
	
	public void setLogin(String name) {
		login = name;
	}
	
	public void setPassword(String name) {
		password = name;
	}
	
	public void setValidity(boolean v) {
		isValid = v;
	}
	
	public void setFirstName(String name) {
		firstName = name;
	}

	public void setLastName(String name) {
		lastName = name;
	}
	
	public void setSexe(SexeEnum name) {
		sexe = name;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public void setRegion(Region region) {
		this.region = region;
	}

	public void setCategorie(Categorie categorie) {
		this.categorie = categorie;
	}

	public void setRole(Role role)	{
		this.role = role;
	}

	public void setBirthday(Date d) {
		this.birthday = d;
	}

	public void debiter(int montant){
		money += montant;
	}
	
	@SuppressWarnings("unchecked")
	public static List<User> getAllUser() {
		String query = "select from " + User.class.getName();
		List<User> list = (List<User>) PMF.executeQuery(query);		
		return list;
	}

	@SuppressWarnings("unchecked")
	public static List<User> getAllMembers() {
		String query = "select from " + User.class.getName() + " WHERE role=='" + Role.MEMBRE + "'";		
		List<User> list = (List<User>) PMF.executeQuery(query);
		return list;
	}
}
